package com.lb.activity.website.controller;

import com.lb.activity.po.DictionaryData;
import com.lb.activity.po.WebArticle;
import com.lb.activity.sys.service.DictionaryService;
import com.lb.activity.util.ResultUtil;
import com.lb.activity.vo.ArticleVo;
import com.lb.activity.vo.PageVo;
import com.lb.activity.website.service.TextService;
import org.activiti.engine.impl.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author 001030
 */
@RestController
public class TextController {

    @Autowired
    private TextService textService;

    @Autowired
    private DictionaryService dictionaryService;

    /**
     * 列表页
     * @return
     */
    @GetMapping("text")
    public ModelAndView text(){
        return new ModelAndView("text/text");
    }

    /**
     * 到添加页面
     * @return
     */
    @GetMapping("toAddText")
    public ModelAndView toAddText(){
        List<DictionaryData> remen_biaoqian = dictionaryService.dictionaryDatas("remen_biaoqian");
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("text/text-add");
        modelAndView.addObject("remen_biaoqian",remen_biaoqian);
        return modelAndView;
    }

    /**
     * 上传文件
     * @param request
     * @return
     */
    @PostMapping("uploadImg")
    public ResultUtil uploadImg(HttpServletRequest request){
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        String src = textService.uploadFileAndReturnFilePath(multipartRequest);
        ConcurrentHashMap<String,String> map = new ConcurrentHashMap<>(1);
        map.put("src",src);
        return ResultUtil.build(0,"",0,map);
    }

    /**
     * 添加文章
     * @param webArticle
     * @param content
     * @return
     */
    @PostMapping("addText")
    public ResultUtil addText(WebArticle webArticle,String content){
        ResultUtil resultUtil = textService.addText(webArticle,content);
        return resultUtil;
    }

    /**
     * 列表集合
     * @param pageVo
     * @return
     */
    @GetMapping("texts")
    public Object texts(PageVo pageVo){
        ResultUtil resultUtil = textService.texts(pageVo);
        return resultUtil;
    }

    /**
     * 删除数据
     * @param id
     * @return
     */
    @PostMapping("removeTextData")
    public Object removeTextData(Long id){
        ResultUtil resultUtil = textService.removeData(id);
        return resultUtil;
    }

    /**
     * 修改数据页面
     * @param id
     * @return
     */
    @GetMapping("toModifyTextData")
    public ModelAndView toModifyTextData(Long id){
        List<DictionaryData> remen_biaoqian = dictionaryService.dictionaryDatas("remen_biaoqian");
        ArticleVo articleVo = textService.viewDataById(id);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("text/text-modify");
        modelAndView.addObject("articleVo",articleVo);
        modelAndView.addObject("remen_biaoqian",remen_biaoqian);
        return modelAndView;
    }

    /**
     * 更新
     * @param articleVo
     * @param content
     * @return
     */
    @PostMapping("updateText")
    public ResultUtil updateText(ArticleVo articleVo,String content){
        ResultUtil resultUtil = textService.updateText(articleVo,content);
        return resultUtil;
    }


    @PostMapping("toLockTopText")
    public ResultUtil toLockTopText(ArticleVo articleVo){
        ResultUtil resultUtil = textService.toLockTopText(articleVo);
        return resultUtil;
    }

    @PostMapping("toLockHotText")
    public ResultUtil toLockHotText(ArticleVo articleVo){
        ResultUtil resultUtil = textService.toLockHotText(articleVo);
        return resultUtil;
    }



}
